package com.coffee.service.impl;

import com.coffee.conmmon.BaseResult;
import com.coffee.dao.UserDaoMapper;
import com.coffee.entity.User;
import com.coffee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

/**
 * 2 * @Author: 刘佳乐 :面向百度编程
 * 3 * @Date: 2020/12/8 0:37
 * 4
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDaoMapper userDaoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;
    @Override
    public BaseResult findAll() {
        List<User> users = userDaoMapper.findAll();
        BaseResult baseResult=new BaseResult();
        if(users!=null){
            baseResult.setCode(200);
            baseResult.setData(users);
            baseResult.setMessage("查找成功");
            return baseResult;
        }
        baseResult.setMessage("查找失败");
        baseResult.setCode(201);
        return baseResult;
    }

    @Override
    public BaseResult login(User user, HttpServletRequest request) {
        User byUname = userDaoMapper.findByUname(user);
        BaseResult baseResult=new BaseResult();
        if(byUname!=null){
            baseResult.setCode(200);
            baseResult.setMessage("登陆成功");
            HttpSession session = request.getSession();
            session.setAttribute(user.getUname(),byUname);
            return baseResult;
        }
        baseResult.setCode(201);
        baseResult.setMessage("登陆失败，请重新登录");
        return baseResult;
    }

    @Override
    public BaseResult sendMail(String mail, HttpServletRequest request) {
        BaseResult baseResult=new BaseResult();
//        mail="1051642283@qq.com";
        if (mail!=null){
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(mail);
            simpleMailMessage.setSubject("星巴克注册验证码");
            Random random = new Random();
            StringBuffer code = new StringBuffer();
            for (int i =0;i<4;i++){
                int i1 = random.nextInt(10);
                code.append(i1);
            }
            simpleMailMessage.setText(code.toString());
            javaMailSender.send(simpleMailMessage);
            //存储到session中
            HttpSession session = request.getSession();
            session.setAttribute(mail,code.toString());
            baseResult.setCode(200);
            baseResult.setMessage("验证码发送成功");
            return baseResult;
        }
        baseResult.setCode(201);
        baseResult.setMessage("验证码接受失败，邮箱为空");
        return baseResult;
    }

    @Override
    public BaseResult registry(User user,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute(user.getEmail());
        BaseResult baseResult=new BaseResult();
        if(code.equalsIgnoreCase(user.getCode())){
            User byUname = userDaoMapper.findByUname(user);
            if(byUname==null){
            Integer registry = userDaoMapper.registry(user);
                if(registry==1){
                baseResult.setCode(200);
                baseResult.setMessage("注册成功");
                return baseResult;
                }else {
                    baseResult.setCode(201);
                    baseResult.setMessage("注册失败");
                }
            }else{
                baseResult.setCode(202);
                baseResult.setMessage("用户名已存在");
                return baseResult;
            }
        }
        baseResult.setCode(203);
        baseResult.setMessage("验证码输入错误");
        return baseResult;
    }
}
